/*
题目链接:https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-i/description/?envType=daily-question&envId=2025-04-02
*/
//题解代码:

class Solution {
public:
    #define ll long long
    long long maximumTripletValue(vector<int>& nums) {
        ll ans = 0; int n = nums.size();
        vector<int> suf(n+1,0);
        // 预处理后缀最大值
        for(int i=n-1;i>=0;--i){
            suf[i] = max(suf[i+1],nums[i]);
        }
        for(int premax=nums[0],submax=0,i=1;i<n-1;++i){
            submax = max(submax,premax-nums[i]);
            ans = max(ans,1LL*submax*suf[i+1]);
            premax = max(premax,nums[i]);
        }
        return ans;
    }
};
